define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'gpswarning/index' + location.search,
                    add_url: 'gpswarning/add',
                    edit_url: 'gpswarning/edit',
                    del_url: 'gpswarning/del',
                    multi_url: 'gpswarning/multi',
                    table: 'gpswarning',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'platenum', title: __('Platenum')},
                        {field: 'gpstime', title: __('Gpstime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'longitude', title: __('Longitude')},
                        // {field: 'latitude', title: __('Latitude')},
                        {field: 'address', title: __('Address')},
                        {field: 'alarminfo', title: __('Alarminfo')},
                        {field: 'tel_times', title: '电话通知次数'},
                        {field: 'wechat_times', title: '微信通知次数'},
                        {field: 'sms_times', title: '短信通知次数'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                         buttons: [{
                                    name: 'phone', 
                                    icon: 'fa fa-phone',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'Gpswarning/tel'
                                },{
                                    name: 'wechat', 
                                    icon: 'fa fa-wechat',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'Gpswarning/wechat'
                                },{
                                    name: 'sms', 
                                    icon: 'fa fa-envelope',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'Gpswarning/sms'
                                }], 
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 同步数据
            $(document).on("click", ".btn-sysn", function () {
                 Layer.open({
                        content: Template("sysntpl", {}),
                        area: ['430px', '350px'],
                        title: '同步数据',
                        resize: false,
                        btn: ['同步数据', '取消'],
                        yes: function (index, layero) {
                            var arr = new Array(); 
                            $('input[name="sourceSelect"]:checked').each(function(i){
                               console.log($(this).val());
                               arr[i] = $(this).val(); 
                            }); 
                            var json = {};
                            if(arr){
                                json['source'] = arr.join(','); 
                                json['_method'] = 'POST'; 
                            } 
                            Fast.api.ajax({
                                url:'Gpswarning/sysn',  
                                dataType: 'jsonp',
                                data: json
                            }, function (data, ret) {
                                // Controller.api.userinfo.set(data);
                                Layer.closeAll(); 
                                Layer.alert(ret.msg);
                            }, function (data, ret) { 
                                console.log(data);
                                console.log(ret);
                                Layer.closeAll(); 
                            });
                        },
                        btn2: function () {
                            Layer.closeAll();
                            return false;
                        } 
                    });  
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        sysn: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});